using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Services.Security;

public partial class Private_CMS_main_Content : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxCMSAccess"; } }
	#endregion
	#region ViewState and Properties
	/// <summary>
	/// Store the parent ID being displayed
	/// </summary>
	protected int ParentID
	{
		get
		{
			object o = ViewState["PID"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["PID"] = value;
		}
	}
	/// <summary>
	/// Store the selected ID as necessary
	/// </summary>
	protected int SelectedID
	{
		get
		{
			object o = ViewState["SID"];
			if (o == null) return -1;
			return (int)o;
		}
		set
		{
			ViewState["SID"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Load selection data if needed
			SelectedID = GetAttribute("selectID", -1);

			// Load parent data
			ParentID = GetAttribute("parentID", 0);
			// If no path passed in, user last known path from registry
			if (ParentID == 0)
				ParentID = UserSetting("CMSLASTPATH", -1);

			// Load node if necessary
			ContentManager cms = new ContentManager();
			ContentObject obj = cms.AdxGetByID(ParentID);
			if (obj == null) ParentID = -1;

			// Ensure node exists - if not, default to root
			if( ParentID == -1 )
			{
				mainPanel.Title = "root";
			}
			else
			{
				mainPanel.Title = obj.NodeTitle;
			}
			
			// Bind the data
			BindData();

			// Reselect if needed
			if (SelectedID != -1)
			{
				for (int c = 0; c < _data.DataKeys.Count; c++)
				{
					int rowKey = (int)_data.DataKeys[c];
					if (rowKey == SelectedID)
					{
						_data.SelectedIndex = c;
						break;
					}
				}
			}

			// Ask tree to reload as necessary
			if ( GetAttribute("reload", "") != "" )
			{
				ClientScript.RegisterStartupScript(typeof(Private_CMS_main_Content),
					"treerefresh",
					"this.parent.refreshTree();", true);

				ClientScript.RegisterStartupScript(typeof(Private_CMS_main_Content),
					"actionsrefresh",
					"this.parent.refreshActions();", true);
			}
		}
	}
	#endregion
	#region Page_Init Event
	/// <summary>
	/// Capture events
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Init(object sender, EventArgs e)
	{
		_data.SelectedIndexChanged += new EventHandler(_data_SelectedIndexChanged);
		_data.SortCommand += new DataGridSortCommandEventHandler(_data_SortCommand);
		_data.SortDirectionCommand += new Deepcode.Flux.Core.UI.Controls.StandardDataGrid.OnSortDirectionCommand(_data_SortDirectionCommand);
	}
	#endregion
	#region Data Binding
	/// <summary>
	/// Load clients into the grid
	/// </summary>
	private void BindData()
	{
		ContentManager cms = new ContentManager();
		ContentObjectSortOrder csort = ContentObjectSortOrder.RANK;
		if( _data.CurrentSort == "Rank" ) csort = ContentObjectSortOrder.RANK;
		if( _data.CurrentSort == "NodeTitle" ) csort = ContentObjectSortOrder.TITLE;
		_data.DataSource = cms.AdxGetByParentID( this.ParentID, csort, _data.CurrentSortDirection );
		_data.DataBind();
	}

	void _data_SortDirectionCommand(object source, string SortExpression, Deepcode.Flux.Core.Data.DataSortDirection Direction)
	{
		BindData();
	}

	void _data_SortCommand(object source, DataGridSortCommandEventArgs e)
	{
		BindData();
	}
	#endregion
	#region User Interaction
	/// <summary>
	/// Selected file has changed
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	void _data_SelectedIndexChanged(object sender, EventArgs e)
	{
		if (_data.SelectedIndex == -1)
		{
			// Nothing selected
			SelectedID = -1;
			// open actions panel for the selected folder
			ClientScript.RegisterStartupScript(typeof(Private_CMS_main_Content),
				"loadActions",
				"this.parent.loadActions('" + this.ParentID + "');",
				true);
		}
		else
		{
			int selection = (int) _data.DataKeys[_data.SelectedIndex];
			SelectedID = selection;

			// open actions panel for the selected folder
			ClientScript.RegisterStartupScript(typeof(Private_CMS_main_Content),
				"loadActions",
				"this.parent.loadActions('" + selection.ToString() + "');",
				true);
		}
	}
	#endregion
	#region Custom Databinders
	/// <summary>
	/// Get icon HTML based on the status of the item passed in
	/// </summary>
	/// <param name="o"></param>
	/// <returns></returns>
	public string GetIcon(object o)
	{
		if( o == null  ) return "";
		if( !(o is ContentObject) ) return "";

		ContentObject obj = (ContentObject) o;
		string basepath = "~/admin/private/cms/main/img/";

		switch( obj.WorkflowStatus )
		{
			case ContentObjectStatus.CHECKEDOUT:
				if( obj.FK_CheckedOutTo == CurrentUser.PK_ID )
					return basepath + "stateCheckoutYou.gif";
				else
					return basepath + "stateCheckout.gif";
			case ContentObjectStatus.CHECKEDINAWAITING:
				if( obj.FK_CheckedInBy == CurrentUser.PK_ID )
					return basepath + "stateCheckinYou.gif";
				else
					return basepath + "stateCheckin.gif";
			case ContentObjectStatus.SUBMITTED:
				return basepath + "stateSubmitted.gif";
			case ContentObjectStatus.COMPLETE:
				return basepath + "statePublished.gif";
		}

		return basepath + "statePublished.gif";
	}
	/// <summary>
	/// Get ALT text for the icon
	/// </summary>
	/// <param name="o"></param>
	/// <returns></returns>
	public string GetAlt(object o)
	{
		if (o == null) return "";
		if (!(o is ContentObject)) return "";

		ContentObject obj = (ContentObject)o;

		switch (obj.WorkflowStatus)
		{
			case ContentObjectStatus.CHECKEDOUT:
				if (obj.FK_CheckedOutTo == CurrentUser.PK_ID)
					return "Checked out to you";
				else
					return "Checked out to someone else";

			case ContentObjectStatus.CHECKEDINAWAITING:
				if (obj.FK_CheckedInBy == CurrentUser.PK_ID)
					return "Checked in by you, awaiting submission";
				else
					return "Checked in, awaiting submission";
			
			case ContentObjectStatus.SUBMITTED:
				return "Submitted for approval, awaiting update";

			case ContentObjectStatus.COMPLETE:
				return "Approved and Published";
		}
		return "Unknown status";
	}
	#endregion
}
